package com.drinks.yiniang.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.listview.WineListAdapter;
import com.drinks.yiniang.entity.goods.GoodsObject;
import com.drinks.yiniang.entity.user.wine.WineListJson;
import com.drinks.yiniang.io.api.WineListApiIO;
import com.drinks.yiniang.ui.activity.user.wine.AddToWineListActivity;
import com.drinks.yiniang.ui.activity.user.wine.CreateWineListActivity;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.Constants;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.dialog.BaseDialogFragment;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author han
 * @date 2019/11/12
 */
public class WineListDialog extends BaseDialogFragment implements View.OnClickListener, AdapterView.OnItemClickListener {

    private final int REQ_CREATE_CODE = 0x01; //新建酒单
    private final int REQ_ADD_CODE = 0x02; //添加到酒单

    //新建酒单
    private TextView tv_wine_list_dialog_add;
    //就单列表
    private ListView lv_wine_list_dialog;
    //进度
    private ContentLoadingProgressBar progress_wine_list_dialog;

    private WineListAdapter mWineListAdapter;

    private List<WineListJson.WineList> mList;

    //商品id
    private GoodsObject mGoodsObject;

    public static WineListDialog newInstance(GoodsObject obj) {
        //把所有需要传递的数据都放在Bundle中
        Bundle bundle = new Bundle();

        bundle.putSerializable(Constants.KeyObject, obj);

        WineListDialog dialog = new WineListDialog();

        dialog.setArguments(bundle);

        return dialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 定义Dialog布局和参数
        mDialog = new Dialog(getActivity(), R.style.ActionSheetDialogStyle);

        // 获取Dialog布局
        View mView = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_wine_list, null);

        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        mDialog.setContentView(mView);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);

        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平

        dialogWindow.setAttributes(lp);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        tv_wine_list_dialog_add = mView.findViewById(R.id.tv_wine_list_dialog_add);

        lv_wine_list_dialog = mView.findViewById(R.id.lv_wine_list_dialog);

        progress_wine_list_dialog = mView.findViewById(R.id.progress_wine_list_dialog);

        tv_wine_list_dialog_add.setOnClickListener(this);

        lv_wine_list_dialog.setOnItemClickListener(this);

        initData();

        return mDialog;
    }

    private void initData() {
        mContext = getActivity();
        mList = new ArrayList<>();

        if(getArguments() != null) {
            mGoodsObject = (GoodsObject) getArguments().getSerializable(Constants.KeyObject);
        }

        mWineListAdapter = new WineListAdapter(mContext, mList);

        lv_wine_list_dialog.setAdapter(mWineListAdapter);

        getWineList();
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_wine_list_dialog_add) {//添加酒单
            startActivityForResult(new Intent(mContext, CreateWineListActivity.class), REQ_CREATE_CODE);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(CommonUtils.isArrayIndexOutOfBounds(mList, position)) {
            return;
        }

        Intent intent = new Intent(mContext, AddToWineListActivity.class);
        intent.putExtra(Constants.KeyObject, mGoodsObject);
        intent.putExtra(Constants.KeyWineListId, mList.get(position).getId());
        intent.putExtra(Constants.KeyTitle, mList.get(position).getTitle());
        startActivityForResult(intent, REQ_ADD_CODE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Constants.RESULT_CODE_NORMAL) {
            switch (requestCode) {
                case REQ_CREATE_CODE://刷新酒单列表
                    getWineList();
                    break;
                case REQ_ADD_CODE://添加到酒单
                    dismiss();
                    break;
                default:
                    break;
            }
        }
    }

    //获取酒单列表
    private void getWineList() {
        progress_wine_list_dialog.show();

        WineListApiIO.getInstance().getWineList(new APIRequestCallback<WineListJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(WineListJson res) {
                if(mList == null) {
                    return;
                }

                mList.clear();
                mList.addAll(res.getData());
                mWineListAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                if(progress_wine_list_dialog != null) progress_wine_list_dialog.hide();
            }
        });
    }

    @Override
    public void onDestroy() {
        mDialog = null;
        super.onDestroy();
    }

    public interface OnWineListClickListener {
        void onWineListAddClick(View v);

        void onWineListItemClick(View v, int position);
    }
}
